{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import wave\n",
    "import pprint as pp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "group: 1089\n",
      "\t speaker: 134686\n",
      "\t speaker: 134691\n",
      "group: 1188\n",
      "\t speaker: 133604\n",
      "group: 121\n",
      "\t speaker: 121726\n",
      "\t speaker: 123852\n",
      "\t speaker: 123859\n",
      "\t speaker: 127105\n",
      "group: 1221\n",
      "\t speaker: 135766\n",
      "\t speaker: 135767\n",
      "group: 1284\n",
      "\t speaker: 1180\n",
      "\t speaker: 1181\n",
      "\t speaker: 134647\n",
      "group: 1320\n",
      "\t speaker: 122612\n",
      "\t speaker: 122617\n",
      "group: 1580\n",
      "\t speaker: 141083\n",
      "\t speaker: 141084\n",
      "group: 1995\n",
      "\t speaker: 1826\n",
      "\t speaker: 1836\n",
      "\t speaker: 1837\n",
      "group: 2094\n",
      "\t speaker: 142345\n",
      "group: 2300\n",
      "\t speaker: 131720\n",
      "group: 237\n",
      "\t speaker: 126133\n",
      "\t speaker: 134493\n",
      "\t speaker: 134500\n",
      "group: 260\n",
      "\t speaker: 123286\n",
      "\t speaker: 123288\n",
      "\t speaker: 123440\n",
      "group: 2830\n",
      "\t speaker: 3979\n",
      "\t speaker: 3980\n",
      "group: 2961\n",
      "\t speaker: 960\n",
      "\t speaker: 961\n",
      "group: 3570\n",
      "\t speaker: 5694\n",
      "\t speaker: 5695\n",
      "\t speaker: 5696\n",
      "group: 3575\n",
      "\t speaker: 170457\n",
      "group: 3729\n",
      "\t speaker: 6852\n",
      "group: 4077\n",
      "\t speaker: 13751\n",
      "\t speaker: 13754\n",
      "group: 4446\n",
      "\t speaker: 2271\n",
      "\t speaker: 2273\n",
      "\t speaker: 2275\n",
      "group: 4507\n",
      "\t speaker: 16021\n",
      "group: 4970\n",
      "\t speaker: 29093\n",
      "\t speaker: 29095\n",
      "group: 4992\n",
      "\t speaker: 23283\n",
      "\t speaker: 41797\n",
      "\t speaker: 41806\n",
      "group: 5105\n",
      "\t speaker: 28233\n",
      "\t speaker: 28240\n",
      "\t speaker: 28241\n",
      "group: 5142\n",
      "\t speaker: 33396\n",
      "\t speaker: 36377\n",
      "\t speaker: 36586\n",
      "\t speaker: 36600\n",
      "group: 5639\n",
      "\t speaker: 40744\n",
      "group: 5683\n",
      "\t speaker: 32865\n",
      "\t speaker: 32866\n",
      "\t speaker: 32879\n",
      "group: 61\n",
      "\t speaker: 70968\n",
      "\t speaker: 70970\n",
      "group: 672\n",
      "\t speaker: 122797\n",
      "group: 6829\n",
      "\t speaker: 68769\n",
      "\t speaker: 68771\n",
      "group: 6930\n",
      "\t speaker: 75918\n",
      "\t speaker: 76324\n",
      "\t speaker: 81414\n",
      "group: 7021\n",
      "\t speaker: 79730\n",
      "\t speaker: 79740\n",
      "\t speaker: 79759\n",
      "\t speaker: 85628\n",
      "group: 7127\n",
      "\t speaker: 75946\n",
      "\t speaker: 75947\n",
      "group: 7176\n",
      "\t speaker: 88083\n",
      "\t speaker: 92135\n",
      "group: 7729\n",
      "\t speaker: 102255\n",
      "group: 8224\n",
      "\t speaker: 274381\n",
      "\t speaker: 274384\n",
      "group: 8230\n",
      "\t speaker: 279154\n",
      "group: 8455\n",
      "\t speaker: 210777\n",
      "group: 8463\n",
      "\t speaker: 287645\n",
      "\t speaker: 294825\n",
      "\t speaker: 294828\n",
      "group: 8555\n",
      "\t speaker: 284447\n",
      "\t speaker: 284449\n",
      "\t speaker: 292519\n",
      "group: 908\n",
      "\t speaker: 157963\n",
      "\t speaker: 31957\n"
     ]
    }
   ],
   "source": [
    "def handler(data_dir, out_file):\n",
    "    sentences = []\n",
    "    durations = []\n",
    "    keys = []\n",
    "    \n",
    "    for group in os.listdir(data_dir):\n",
    "        print(\"group:\", group)\n",
    "        if group == '.DS_Store': continue\n",
    "            \n",
    "        group_path = os.path.join(data_dir, group)\n",
    "        for speaker in os.listdir(group_path):\n",
    "            print(\"\\t speaker:\", speaker)\n",
    "            if group == '.DS_Store': continue\n",
    "                \n",
    "            labels_file = os.path.join(group_path,\n",
    "                                       speaker,\n",
    "                                       '{}-{}.trans.txt'.format(group, speaker))\n",
    "            \n",
    "            for line in open(labels_file):\n",
    "                split = line.strip().split()\n",
    "                file_id = split[0]\n",
    "                sentence = ' '.join(split[1:]).lower()\n",
    "                audio_file = os.path.join(group_path,\n",
    "                                         speaker,\n",
    "                                         file_id)\n",
    "                audio_file += '.wav'\n",
    "                # audio_file += '.flac'\n",
    "                audio = wave.open(audio_file)\n",
    "                duration = float(audio.getnframes())/audio.getframerate()\n",
    "                audio.close()\n",
    "                \n",
    "                keys.append(audio_file)\n",
    "                durations.append(duration)\n",
    "                sentences.append(sentence)\n",
    "                \n",
    "        # pp.pprint(keys)\n",
    "        # pp.pprint(durations)\n",
    "        # pp.pprint(sentences)\n",
    "        with open(output_file, 'w') as out_file:\n",
    "            for i in range(len(sentences)):\n",
    "                line = json.dumps({\n",
    "                    'path':keys[i],\n",
    "                    'time':durations[i],\n",
    "                    'text':sentences[i],\n",
    "                })\n",
    "                \n",
    "                out_file.write(line + '\\n')\n",
    "            \n",
    "        \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    path = \"/Users/chenzomi/Documents/DeepLearnInAction/code/data/LibriSpeech\"\n",
    "    data_directory = \"test-clean/\"\n",
    "    output_file = \"test_corpus.json\"\n",
    "    \n",
    "    data_directory = os.path.join(path, data_directory)\n",
    "    output_file = os.path.join(path, output_file)\n",
    "    \n",
    "    handler(data_directory, output_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
